/*     */ package com.zimbra.cs.filter.jsieve;
/*     */ 
/*     */ import com.zimbra.common.filter.Sieve.Comparator;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.HtmlTextExtractor;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.filter.ZimbraMailAdapter;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mime.MPartInfo;
/*     */ import com.zimbra.cs.mime.Mime;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.PushbackReader;
/*     */ import java.io.Reader;
/*     */ import java.io.StringReader;
/*     */ import java.util.List;
/*     */ import java.util.ListIterator;
/*     */ import javax.mail.internet.MimePart;
/*     */ import org.apache.jsieve.Argument;
/*     */ import org.apache.jsieve.Arguments;
/*     */ import org.apache.jsieve.SieveContext;
/*     */ import org.apache.jsieve.StringListArgument;
/*     */ import org.apache.jsieve.TagArgument;
/*     */ import org.apache.jsieve.exception.SieveException;
/*     */ import org.apache.jsieve.exception.SyntaxException;
/*     */ import org.apache.jsieve.mail.MailAdapter;
/*     */ import org.apache.jsieve.tests.AbstractTest;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class BodyTest
/*     */   extends AbstractTest
/*     */ {
/*     */   static final String CONTAINS = ":contains";
/*     */   static final String COMPARATOR = ":comparator";
/*     */   
/*     */   protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context)
/*     */     throws SieveException
/*     */   {
/*  63 */     String comparison = null;
/*  64 */     boolean caseSensitive = false;
/*  65 */     String key = null;
/*     */     
/*  67 */     ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
/*     */     
/*     */ 
/*     */ 
/*  71 */     if (argumentsIter.hasNext())
/*     */     {
/*  73 */       Object argument = argumentsIter.next();
/*  74 */       if ((argument instanceof TagArgument))
/*     */       {
/*  76 */         String tag = ((TagArgument)argument).getTag();
/*  77 */         if (tag.equals(":contains")) {
/*  78 */           comparison = tag;
/*     */         } else {
/*  80 */           throw new SyntaxException("Found unexpected TagArgument: \"" + tag + "\"");
/*     */         }
/*     */       }
/*     */     }
/*  84 */     if (null == comparison) {
/*  85 */       throw new SyntaxException("Expecting \":contains\"");
/*     */     }
/*     */     
/*  88 */     if (argumentsIter.hasNext())
/*     */     {
/*  90 */       Object argument = argumentsIter.next();
/*  91 */       if ((argument instanceof TagArgument))
/*     */       {
/*  93 */         String tag = ((TagArgument)argument).getTag();
/*  94 */         if (tag.equals(":comparator")) {
/*  95 */           if (argumentsIter.hasNext()) {
/*  96 */             argument = argumentsIter.next();
/*  97 */             if ((argument instanceof StringListArgument)) {
/*  98 */               StringListArgument strList = (StringListArgument)argument;
/*     */               try {
/* 100 */                 caseSensitive = Sieve.Comparator.ioctet == Sieve.Comparator.fromString((String)strList.getList().get(0));
/*     */               } catch (ServiceException e) {
/* 102 */                 throw new SyntaxException(e.getMessage());
/*     */               }
/*     */               
/*     */ 
/* 106 */               if (argumentsIter.hasNext())
/* 107 */                 argument = argumentsIter.next();
/*     */             } else {
/* 109 */               throw new SyntaxException("Found unexpected argument after :comparator");
/*     */             }
/*     */           } else {
/* 112 */             throw new SyntaxException("Unexpected end of arguments");
/*     */           }
/*     */         } else {
/* 115 */           throw new SyntaxException("Found unexpected TagArgument: \"" + tag + "\"");
/*     */         }
/*     */       }
/*     */       
/* 119 */       if ((argument instanceof StringListArgument)) {
/* 120 */         StringListArgument strList = (StringListArgument)argument;
/* 121 */         key = (String)strList.getList().get(0);
/*     */       }
/*     */     }
/* 124 */     if (null == key) {
/* 125 */       throw new SyntaxException("Expecting a string");
/*     */     }
/*     */     
/* 128 */     if (argumentsIter.hasNext()) {
/* 129 */       throw new SyntaxException("Found unexpected argument(s)");
/*     */     }
/* 131 */     return ((mail instanceof ZimbraMailAdapter)) && (test(mail, caseSensitive, key));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   protected void validateArguments(Arguments arguments, SieveContext context) {}
/*     */   
/*     */ 
/*     */   private boolean test(MailAdapter mail, boolean caseSensitive, String substring)
/*     */   {
/* 141 */     ZimbraMailAdapter zimbraMail = (ZimbraMailAdapter)mail;
/* 142 */     ParsedMessage pm = zimbraMail.getParsedMessage();
/* 143 */     if (pm == null) {
/* 144 */       return false;
/*     */     }
/*     */     
/* 147 */     Account acct = null;
/*     */     try {
/* 149 */       acct = zimbraMail.getMailbox().getAccount();
/*     */     } catch (ServiceException e) {
/* 151 */       ZimbraLog.filter.warn("Error in getting account", e);
/*     */     }
/* 153 */     String defaultCharset = acct == null ? null : acct.getPrefMailDefaultCharset();
/*     */     
/* 155 */     for (MPartInfo mpi : pm.getMessageParts()) {
/* 156 */       String cType = mpi.getContentType();
/*     */       
/* 158 */       if (!"attachment".equals(mpi.getDisposition())) { boolean bool;
/* 159 */         if (cType.equals("text/plain")) {
/* 160 */           InputStream in = null;
/*     */           try {
/* 162 */             in = mpi.getMimePart().getInputStream();
/* 163 */             String cthdr = mpi.getMimePart().getHeader("Content-Type", null);
/* 164 */             String charset = null;
/* 165 */             if (cthdr != null)
/* 166 */               charset = Mime.getCharset(cthdr);
/* 167 */             if (charset == null)
/* 168 */               charset = defaultCharset;
/* 169 */             Reader reader = charset == null ? new InputStreamReader(in) : new InputStreamReader(in, charset);
/* 170 */             if (contains(reader, caseSensitive, substring)) {
/* 171 */               return true;
/*     */             }
/*     */           } catch (Exception e) {
/* 174 */             ZimbraLog.filter.warn("Unable to test text body for substring '%s'", substring, e);
/*     */           } finally {
/* 176 */             ByteUtil.closeStream(in);
/*     */           }
/* 178 */         } else if (cType.equals("text/html")) {
/* 179 */           InputStream in = null;
/*     */           
/*     */           try
/*     */           {
/* 183 */             in = mpi.getMimePart().getInputStream();
/* 184 */             String cthdr = mpi.getMimePart().getHeader("Content-Type", null);
/* 185 */             Reader reader = Mime.getTextReader(in, cthdr, defaultCharset);
/* 186 */             String text = HtmlTextExtractor.extract(reader, 1048576);
/* 187 */             if (contains(new StringReader(text), caseSensitive, substring)) {
/* 188 */               return true;
/*     */             }
/*     */           } catch (Exception e) {
/* 191 */             ZimbraLog.filter.warn("Unable to test HTML body for substring '%s'", substring, e);
/*     */           } finally {
/* 193 */             ByteUtil.closeStream(in);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 198 */     return false;
/*     */   }
/*     */   
/*     */   private boolean contains(Reader reader, boolean caseSensitive, String substring) throws IOException
/*     */   {
/* 203 */     int matchIndex = 0;
/* 204 */     if (!caseSensitive)
/* 205 */       substring = substring.toLowerCase();
/* 206 */     PushbackReader pb = new PushbackReader(reader, substring.length());
/* 207 */     char[] substringArray = substring.toCharArray();
/*     */     int c;
/* 209 */     while ((c = getNextChar(pb)) > 0) {
/* 210 */       if (((!caseSensitive) && (substring.charAt(matchIndex) == Character.toLowerCase(c))) || ((caseSensitive) && (substring.charAt(matchIndex) == c)))
/*     */       {
/* 212 */         matchIndex++;
/* 213 */         if (matchIndex == substring.length())
/* 214 */           return true;
/* 215 */       } else if (matchIndex > 0)
/*     */       {
/* 217 */         pb.unread(c);
/*     */         
/* 219 */         pb.unread(substringArray, 1, matchIndex - 1);
/* 220 */         matchIndex = 0;
/*     */       }
/*     */     }
/* 223 */     return false;
/*     */   }
/*     */   
/*     */   private int getNextChar(PushbackReader reader) throws IOException
/*     */   {
/* 228 */     int c = reader.read();
/* 229 */     if ((c != 13) && (c != 10))
/*     */     {
/* 231 */       return c;
/*     */     }
/*     */     
/*     */     do
/*     */     {
/* 236 */       c = reader.read();
/* 237 */     } while ((c == 13) || (c == 10));
/*     */     
/* 239 */     if (c >= 0)
/*     */     {
/* 241 */       reader.unread(c);
/*     */     }
/* 243 */     return 32;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/filter/jsieve/BodyTest.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */